﻿Imports System.Drawing.Drawing2D
Public Class LabelEx
    Dim mTitleText As String = String.Empty
    Public Property TitleText() As String
        Get
            Return mTitleText
        End Get
        Set(ByVal value As String)
            mTitleText = value
            Invalidate()
        End Set
    End Property
    Dim mFirstColor As Color = Color.White
    Public Property FirstColor() As Color
        Get
            Return mFirstColor
        End Get
        Set(ByVal value As Color)
            mFirstColor = value
            Me.BackColor = FirstColor
            Invalidate()
        End Set
    End Property
    Dim mSecondColor As Color = Color.White
    Public Property SecondColor() As Color
        Get
            Return mSecondColor
        End Get
        Set(ByVal value As Color)
            mSecondColor = value
            Invalidate()
        End Set
    End Property
    Private mTextAlign As ContentAlignment = ContentAlignment.MiddleCenter
    Public Property TextAlign() As ContentAlignment
        Get
            Return mTextAlign
        End Get
        Set(ByVal value As ContentAlignment)
            mTextAlign = value
            Me.Invalidate()
        End Set
    End Property
    Private Sub LabelEx_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim mWidth As Integer
        mWidth = Me.Width
        If mWidth Mod 2 = 1 Then mWidth += 1
        Me.BackColor = FirstColor
        Dim g As Graphics = e.Graphics
        Dim lg As LinearGradientBrush = New LinearGradientBrush(New Rectangle(0, 0, mWidth, Me.Height), mFirstColor, mSecondColor, LinearGradientMode.Horizontal)
        Dim regLeft As GraphicsPath = New GraphicsPath
        regLeft.AddRectangle(New Rectangle(0, 0, mWidth, Me.Height))
        g.FillPath(lg, regLeft)
        Dim sf As StringFormat = StringFormatAlignment(mTextAlign)
        g.DrawString(mTitleText, Me.Font, New SolidBrush(Me.ForeColor), New Rectangle(0, 0, Me.Width, Me.Height), sf)
    End Sub
    Private Function StringFormatAlignment(ByVal textalign As ContentAlignment) As StringFormat
        Dim sf As StringFormat = New StringFormat
        Select Case (textalign)
            Case ContentAlignment.TopLeft, ContentAlignment.TopCenter, ContentAlignment.TopRight
                sf.LineAlignment = StringAlignment.Near
            Case ContentAlignment.MiddleLeft, ContentAlignment.MiddleCenter, ContentAlignment.MiddleRight
                sf.LineAlignment = StringAlignment.Center
            Case ContentAlignment.BottomLeft, ContentAlignment.BottomCenter, ContentAlignment.BottomRight
                sf.LineAlignment = StringAlignment.Far
        End Select
        Select Case (textalign)
            Case ContentAlignment.TopLeft, ContentAlignment.MiddleLeft, ContentAlignment.BottomLeft
                sf.Alignment = StringAlignment.Near
            Case ContentAlignment.TopCenter, ContentAlignment.MiddleCenter, ContentAlignment.BottomCenter
                sf.Alignment = StringAlignment.Center
            Case ContentAlignment.TopRight, ContentAlignment.MiddleRight, ContentAlignment.BottomRight
                sf.Alignment = StringAlignment.Far
        End Select
        Return sf
    End Function
    Private Sub LabelEx_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Invalidate()
    End Sub
    Public Sub New()
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.SetStyle(ControlStyles.Selectable, False)
    End Sub
End Class
